﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TfsBugger.Core;
using TfsBugger.Util.Ui;

namespace TfsBugger.Controls
{
    /// <summary>
    /// Interaction logic for BuggerTab.xaml
    /// </summary>
    public partial class BuggerTab : UserControl
    {
        public BuggerTab(Bugger b)
        {
            InitializeComponent();
            m_bugger = b;
            m_bugger.PropertyChanged += OnBuggerPropertyChanged;
        }


        private Bugger m_bugger;


        private TabItem MyTab { get; set; }
        private bool ContentLoaded { get; set; }


        public Bugger Bugger
        {
            get { return m_bugger; }
        }


        private void OnControlLoaded(object sender, RoutedEventArgs e)
        {
            MyTab = this.FindFirstAncestorOfType<TabItem>();
            if (null != MyTab)
                MyTab.Header = Bugger.Name;

            if (ContentLoaded)
                return;

            UIElement ui = BuggerUiFactory.CreateUi(Bugger);
            DockPanel.SetDock(ui, Dock.Top);
            this.dpContent.Children.Add(ui);
            ContentLoaded = true;
        }


        private void OnBuggerPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName.ToLower() == "name" && null != MyTab)
                MyTab.Header = Bugger.Name;
        }


        private void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            if (null == MyTab)
                return;

            TabControl tabPar = MyTab.FindFirstAncestorOfType<TabControl>();
            if (null != tabPar)
                tabPar.Items.Remove(MyTab);
        }
    }
}
